package com.jumi.microservice.stock.factory;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.domain.GoodsStockDO;
import com.jumi.microservice.domain.GoodsStockLogDO;
import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.domain.OrderItemDTO;
import com.jumi.microservice.mapper.GoodsStockLogMapper;
import com.jumi.microservice.mapper.GoodsStockMapper;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import com.jumi.microservice.stock.StockUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存更新命令工厂的父类
 *
 * @author Administrator
 */
public abstract class AbstractStockOrderUpdaterFactory<T> implements StockUpdaterFactory<T> {

    private static final Logger logger = LoggerFactory.getLogger(AbstractStockOrderUpdaterFactory.class);

    /**
     * 商品库存
     */
    protected GoodsStockMapper goodsStockMapper;

    /**
     * 库存流水
     */
    protected GoodsStockLogMapper goodsStockLogMapper;

    /**
     * 商品仓库库存管理模块的mapper组件
     */
    protected GoodsWarehouseStockMapper goodsWarehouseStockMapper;

    /**
     * 构造函数
     *
     * @param goodsStockMapper 商品库存管理模块的mapper组件
     */
    public AbstractStockOrderUpdaterFactory(GoodsWarehouseStockMapper goodsStockMapper) {
        this.goodsWarehouseStockMapper = goodsStockMapper;
    }

    /**
     * 构造函数 多参
     * @param goodsStockMapper
     * @param goodsStockLogMapper
     * @param goodsWarehouseStockMapper
     */
    public AbstractStockOrderUpdaterFactory(GoodsStockMapper goodsStockMapper, GoodsStockLogMapper goodsStockLogMapper, GoodsWarehouseStockMapper goodsWarehouseStockMapper){
        this.goodsStockMapper = goodsStockMapper;
        this.goodsStockLogMapper = goodsStockLogMapper;
        this.goodsWarehouseStockMapper = goodsWarehouseStockMapper;
    }

    /**
     * 创建库存更新命令
     */
    @Override
    public StockUpdater create(T parameter) {
        try {
            OrderItemDTO dto = (OrderItemDTO) parameter;
            GoodsStockLogDO goodsStockLog = getGoodsStockLog(dto.getStockLogId());
            GoodsStockDO goodsStock = getGoodsStock(dto.getGoodsSkuId());
            GoodsWarehouseStockDO goodsWarehouseStockDOS = getGoodsWareStocks(dto.getGoodsSkuId(), dto.getActualWarehouseId());
            return create(goodsStock, goodsStockLog, goodsWarehouseStockDOS, parameter);
        } catch (Exception e) {
            logger.error("error", e);
        }
        return null;
    }


    /**
     * 商品库存DO对象
     * @return 商品库存DO对象集合
     */
    private GoodsStockDO getGoodsStock(Long goodsSkuId){
        GoodsStockDO goodsStock = goodsStockMapper.selectOne(
                Wrappers.<GoodsStockDO>lambdaQuery()
                        .eq(GoodsStockDO::getGoodsSkuId,goodsSkuId));
        return goodsStock;
    }

    /**
     * 仓库库存DO对象
     * @return 仓库库存DO对象集合
     */
    private GoodsWarehouseStockDO getGoodsWareStocks(Long skuId, Long wareId){
        GoodsWarehouseStockDO goodsWarehouseStock = goodsWarehouseStockMapper.selectOne(
                Wrappers.<GoodsWarehouseStockDO>lambdaQuery()
                        .eq(GoodsWarehouseStockDO::getGoodsSkuId, skuId)
                        .eq(GoodsWarehouseStockDO::getWarehouseId, wareId));
        return goodsWarehouseStock;
    }

    /**
     * 仓库库存DO对象
     * @return 仓库库存DO对象集合
     */
    private GoodsStockLogDO getGoodsStockLog(Long stockLogId){
        GoodsStockLogDO goodsStockLogDO = goodsStockLogMapper.selectById(stockLogId);
        return goodsStockLogDO;
    }

    /**
     * 创建库存更新命令
     *
     * @param parameter   参数
     * @param goodsStockDO 商品库存对象
     * @param goodsStockLogDO 库存流水对象
     * @param goodsWarehouseStockDOS 商品仓库库存DO对象集合
     * @return 库存更新命令
     * @throws Exception
     */
    protected abstract StockUpdater create(GoodsStockDO goodsStockDO, GoodsStockLogDO goodsStockLogDO, GoodsWarehouseStockDO goodsWarehouseStockDOS, T parameter) throws Exception;

}
